﻿//"use strict";

/// <reference path="jquery.js" />
/// <reference path="jquery-3.3.1.intellisense.js" />
/// <reference path="Liber.Base.js" />
document.getElementsByTagName('html')[0].style.fontSize = (document.body.clientWidth / 992 * 26) + 'px';
if (!window.SoWeChat) {
    window.SoWeChat = {
        Login: function (fnOnSuccess, fnOnFail) {
            /// <summary>
            /// 微信页面登录过程
            /// </summary>
            /// <param name="fnOnSuccess" type="type">登录成功回调</param>
            /// <param name="fnOnFail" type="type">登录失败时的回调【如不提供，默认跳转到帐号注册页面】</param>
            $$.Login(fnOnSuccess, function (err) {
                /*开始微信登录过程*/
                var wxuid = $.Cookie("wxuid");
                if (!wxuid) {
                    wxuid = window.location.hash.substr(1);
                }
                if (wxuid) {
                    /*用户已授权，由微信回调当前页面，开始用用户的openid尝试登录*/
                    var ur = {
                        key: wxuid,
                        pwd: '',
                        open: 'WeChat',
                    };
                    $$.POST("/SoBiz/Login", ur, function (rtn) {
                        if (rtn.IsPositive) {
                            /*登录成功*/
                            $$.User = rtn.Data;
                            fnOnSuccess && fnOnSuccess($$.User);
                        }
                        else {
                            /*登录失败，跳转到注册页面*/
                            fnOnFail ? fnOnFail(rtn) : window.location = "/Acnt/wx_TrdRegister.html?openuid=" + wxuid;
                        }
                    });
                }
                else {
                    /*请求用户授权，等待微信回调当前页面*/
                    window.location = "/WeChat/OAuthBegin";
                }
            });
        },
        JsAuth: function (permision, onFail) {
            $$.GetJSON("/WeChat/JSAuth?page=" + window.location.href.split('#')[0], null, function (rtn) {
                if (rtn.IsPositive) {
                    var cfg = {
                        debug: false,
                        appId: rtn.Data.a,
                        timestamp: rtn.Data.t,
                        nonceStr: rtn.Data.n,
                        signature: rtn.Data.s,
                        jsApiList: permision
                    };
                    SoWeChat.wxConfig(cfg, 0);
                }
                else {
                    onFail || err("授权失败", "未能获取您的授权，页面拍照等功能无法使用");
                }
            });
        },
        wxConfig: function (cfg, n) {
            if (wx) {
                wx.config(cfg);
            }
            else if (n < 3) {
                setTimeout(SoWeChat.wxConfig, 1000, cfg, n + 1);
            }
            else {
                console.error("微信JSSDK初始化失败");
            }
        }
    };
};
